package com.neusoft.elm.view.impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.view.BusinessView;
import java.util.regex.Pattern;

public class BusinessViewImpl implements BusinessView{

	private Scanner input = new Scanner(System.in);
	
	private static String Rull = "^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(.{6,18})$";//设置正则表达式规则(至少包含一个数字,一个大写字母,一个小写字母)
	
	@Override
	public void getAllBusiness() {
		BusinessDao dao=new BusinessDaoImpl();
		List<Business> list = dao.getBusiness(null,null,null);
		View(list);
	}
	
	@Override
	public void getBusiness() {
		String BusinessName = "";
		String BusinessAddress = "";
		String BusinessId = "";		
	
		System.out.println("是否需要根据商家编号进行查询(y/n)");
		String inputStr = input.next();		
		if(inputStr.equals("y")) {
			System.out.println("请输入该商家编号：");
			BusinessId = input.next();
		}else {
			System.out.println("是否需要根据商家名称关键字进行查询(y/n)：");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入您想查询的商家名称关键词：");
			BusinessName = input.next();
		 }
		
		System.out.println("是否需要根据商家地址关键字进行查询(y/n)：");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入您想查询的商家地址关键词：");
			BusinessName = input.next();
		 }	
		}
		
		

		
		BusinessDao dao=new BusinessDaoImpl();
		List<Business> list = dao.getBusiness(BusinessId,BusinessName,BusinessAddress);
		View(list);
	}
	
	private void View(List<Business> list) {
		
		System.out.println( 
				String.format("%-10s\t%-10s\t%-50s\t%-50s\t%-10s\t%-10s\t",
						"商家编号",
						"商家名称",
						"商家地址",
						"商家介绍",
						"起送费",
						"配送费")
							);
		for(Business b: list) {													//遍历输出所有查询封装到的对象
			System.out.println(
				String.format("%-10s\t%-10s\t%-50s\t%-50s\t%-10s\t%-10s\t",
						b.getBusinessID(),
						b.getBusinessName(),
						b.getBusinessAddress(),
						b.getBusinessExplain(),
						b.getStartPrice(),
						b.getDeliveryPrice())
										);

		}
	}

	@Override
	public void postBusiness() {
		System.out.println("请输入要创立的商家名称：");
		String BusinessName = input.next();
		BusinessDao dao = new BusinessDaoImpl();
		int BusinessId = dao.postBusiness(BusinessName); 
		if(BusinessId>0) {
			System.out.println("创建成功！商家编号为："+ BusinessId);
		}else {
			System.out.println("发生未知错误，新建商家失败！");
		}
	}

	@Override
	public void deleteBusiness() {
		System.out.println("请输入所需删除的商家编号：");
		int BusinessId = input.nextInt();
		BusinessDao dao = new BusinessDaoImpl();
		System.out.println("确认要删除商家编号为"+BusinessId+"的商家吗?(y/n):");
		if(input.next().equals("y")) {
			int result = dao.deleteBusiness(BusinessId); 
			if(result==1) {
				System.out.println("删除商家成功！");
			}else {
				System.out.println("删除商家失败！");
			}
		}
		
	}

	@Override
	public Business login() {
		System.out.println("请输入商家编号：");
		int BusinessId = input.nextInt();
		System.out.println("请输入商家密码：");
		String Password = input.next();
		BusinessDao dao = new BusinessDaoImpl();
		return dao.getBusiness_Id_Password(BusinessId, Password);

	}

	@Override
	public void getBusiness(Integer BusinessId) {
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.getBusiness_Id(BusinessId);
		System.out.println(business);

	}

	@Override
	public void putBusiness(Integer BusinessId) {
		//先用ID调用所需更改商家信息的对象，再进行更新操作
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.getBusiness_Id(BusinessId);
		System.out.println(business); //先将所需更新对象信息显示出来
		String inputStr = "";
		
		System.out.println("是否修改商家名称(y/n): ");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入新的商家名称: ");
			business.setBusinessName(input.next());
		}
		
		System.out.println("是否修改商家介绍(y/n): ");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入新的商家介绍: ");
			business.setBusinessExplain(input.next());
		}
		
		System.out.println("是否修改商家地址(y/n): ");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入新的商家名称: ");
			business.setBusinessName(input.next());
		}
		
		System.out.println("是否修改起送费(y/n): ");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入新的起送费: ");
			business.setStartPrice(input.nextDouble());
		}

		System.out.println("是否修改配送费(y/n): ");
		inputStr = input.next();
		if(inputStr.equals("y")) {
			System.out.println("请输入新的配送费: ");
			business.setDeliveryPrice(input.nextDouble());
		}
		
		int result = dao.putBusiness(business);
		if(result==1) {
			System.out.println("更新成功！ ");
		}
	}

	@Override
	public void patchBusiness_Password(Integer BusinessId) {
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.getBusiness_Id(BusinessId);
		
		System.out.println("\n请输入旧密码： ");
		String old_Password = input.next();
		
		if(business.getPassword().equals(old_Password)) {
		String Password="";
		boolean b = false;
		System.out.println("\n请输入新密码 ");
		while(!b) {
		Password = input.next();
		b = Pattern.matches(Rull, Password);
			if(!b) {
				System.out.println("\n密码格式不正确!(至少包含一个数字,一个大写字母,一个小写字母)\n请重新输入:  ");
			}
		}

		        
		System.out.println("\n请再次输入新密码： ");
		String rep_Password = input.next();	
		if(Password.equals(rep_Password)) {
			int result = dao.patchBusiness_Password(BusinessId, Password);
    		if(result>0) {
    			System.out.println("\n密码修改成功!");

    		}

		}else {
    		System.out.println("\n两次输入不一致，请重新输入");
		}
		
		}else {
			System.out.println("\n输入密码错误！ ");
		}
	}


	
}
